﻿//
// GNS_DailyDate.cpp
// MobileManage
//
// Created by MR.l on 16/2/16.
//
//

#include "GnsClientFrame.h"
#include "GNS_DailyDate.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_Common.h"
#include "GNS_PvpApplyTick.h"
#include "GnsOften.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()

//extern ISocketMgr * _GameSock;

GNS_DailyDate::GNS_DailyDate()
{
    pGNS_DailyDate=0;
    pDailyPlayDate=nullptr;
    DataListView=nullptr;
    TimeText=nullptr;
    pCalendar=nullptr;
    pProName=nullptr;
    GameType=nullptr;
    GameName=nullptr;
    PlayerName=0;
    CurID=0;
    Year=0;
    Mon=0;
    Day=0;
    
}

GNS_DailyDate::~GNS_DailyDate()
{
    
}

GNS_DailyDate* GNS_DailyDate::createLayer()
{
    auto pGns_DailyDate = GNS_DailyDate::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGns_DailyDate);
    return pGns_DailyDate;
}

bool GNS_DailyDate::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    /**
     *  @author MR.L, 16-02-16 11:02:50
     *
     *  获取当前时间
     */
    time_t tt;
    time(&tt);
    struct tm * time;
    time = localtime(&tt);
    
    Mon = (int)time->tm_mon+1;
    Year = time->tm_year+1900;
    Day  =(int)time->tm_mday;

	pDailyPlayDate = GNS_DailyPlayDate::createLayer();
	pDailyPlayDate->setVisible(false);
    return true;
}


void GNS_DailyDate::SetNodeName(std::string CsbName)
{
    pGNS_DailyDate = CSLoader::createNode(CsbName);
    pGNS_DailyDate->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_DailyDate);
    addChild(pGNS_DailyDate,11);
    
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_DailyDate);
    
    auto pBackBtn = static_cast<Button*>(pGNS_DailyDate->getChildByName("PvPApplyTickBtn"));
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_DailyDate::BtnCallBack, this));
    
    DataListView = static_cast<ListView*>(pGNS_DailyDate->getChildByName("DateListView"));
    GameName = static_cast<Text*>(pGNS_DailyDate->getChildByName("Text_3_0"));
    GameType = static_cast<ListView*>(pGNS_DailyDate->getChildByName("GameType"));
    GameType->removeAllItems();

    int x=0;
    for (auto it:(* GNS_PlayerMgr::getInstance()->getGameList())) {
        if (it.iGameType==2) {
            auto pbutton = Button::create("p0Btn.png");
            pbutton->setTitleText(SystemMgr::getInstance()->GbToUTF8(it.szGameIntro));
            pbutton->setTitleColor(Color3B::BLACK);
            pbutton->setTitleFontSize(20);
            pbutton->setTag(7);
            pbutton->addTouchEventListener(CC_CALLBACK_2(GNS_DailyDate::BtnCallBack, this));
            GameType->addChild(pbutton);

			x++;
			if (x == 1)
			{
				GameName->setString(SystemMgr::getInstance()->GbToUTF8(it.szGameIntro));
			}
        }
    }
    
    TimeText = static_cast<Text*>(pGNS_DailyDate->getChildByName("TimeText"));

	if (GNS_PlayerMgr::getInstance()->getGameId() > 0)
	{
		string GameNameTmp = SystemMgr::getInstance()->GbToUTF8(GNS_PlayerMgr::getInstance()->getGameIdInfo()[GNS_PlayerMgr::getInstance()->getGameId()].c_str());
		GameName->setString(GameNameTmp);
	}

    if (GNS_PlayerMgr::getInstance()->getGameProReveRes()->size() > 0) {
		string TimeTextTmp = SystemMgr::getInstance()->GbToUTF8(GNS_PlayerMgr::getInstance()->getGameProReveRes()->at(0)._selDate);
        TimeText->setString(TimeTextTmp);
    }
    GameType->setVisible(false);

    auto pTimeBtn = static_cast<Button*>(pGNS_DailyDate->getChildByName("TimeBtn"));
    pTimeBtn->setTag(2);
	pTimeBtn->setVisible(false);
    //pTimeBtn->addTouchEventListener(CC_CALLBACK_2(GNS_DailyDate::BtnCallBack, this));
    
    auto pTimeSer = static_cast<Button*>(pGNS_DailyDate->getChildByName("Button_8"));
    pTimeSer->setTag(2);
    //pTimeSer->addTouchEventListener(CC_CALLBACK_2(GNS_DailyDate::BtnCallBack, this));
    
    auto typrbtnSrt = static_cast<Button*>(pGNS_DailyDate->getChildByName("Button_8_0"));
    typrbtnSrt->setTag(3);
    //typrbtnSrt->addTouchEventListener(CC_CALLBACK_2(GNS_DailyDate::BtnCallBack, this));
    
    auto gamebtn1 = static_cast<Button*>(pGNS_DailyDate->getChildByName("GameBtn"));
    gamebtn1->setTag(3);
	gamebtn1->setVisible(false);
    //gamebtn1->addTouchEventListener(CC_CALLBACK_2(GNS_DailyDate::BtnCallBack, this));
    
    //TimeText = static_cast<Text*>(pGNS_DailyDate->getChildByName("TimeText"));
    //TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,Day));
    
    pProName = static_cast<Text*>(pGNS_DailyDate->getChildByName("Text_4"));
    
}
void GNS_DailyDate::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:
            {
                if ((int)GNS_PlayerMgr::getInstance()->_NodeChildVec.size()>0) {
                    GNS_PlayerMgr::getInstance()->getGameReveRes()->clear();
                    if (DataListView != nullptr)
                        DataListView->removeAllItems();
                    CMD_CS_GetManToManGameRevenue  pGetGameReven;
                    pGetGameReven._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                    pGetGameReven._uplineCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                    pGetGameReven._gameId=GNS_PlayerMgr::getInstance()->getGameId();
                    pGetGameReven._type=1;
					char TimeDate[11] = { 0 };
					sprintf(TimeDate, "%d-%02d-%02d", Year, Mon, Day);
                    std::string TimeDateTmp = SystemMgr::getInstance()->utf8ToGb(TimeDate);
                    memcpy(pGetGameReven._selDate, TimeDateTmp.c_str(), 11);
					GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMAN_GAME_REVENUE, (char*)&pGetGameReven, sizeof(pGetGameReven));
                    GameType->setVisible(false);
                    
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.erase(1);
					this->removeAllChildren();
                    //this->getParent()->removeChild(this);
					
                }
				auto PromoterNode = GNS_PlayerMgr::getInstance()->_NodeChildVec.at(0);
				PromoterNode->setVisible(true);
                
                break;
            }
            case 2:
            {
					 /* pCalendar = GNS_Calendar::createCalendar();
					  pCalendar->SetCalendBgImage("Calendarbg.png");
					  std::vector<Button *> CalenderBtn;
					  pCalendar->GetTimeButton()->swap(CalenderBtn);
					  for (auto & it : CalenderBtn) {
						  it->setTag(100);
						  it->addTouchEventListener(CC_CALLBACK_2(GNS_DailyDate::TimeButtonCallBack, this));
					  }*/
					  
				pCalendar = GNS_Calendar::createCalendar();
				pCalendar->setButtonsCallback(CC_CALLBACK_2(GNS_DailyDate::TimeButtonCallBack, this));
                break;
            }
            case 3:
            {
                if (GameType->isVisible()==true) {
                    GameType->setVisible(false);
                }else{
                    GameType->setVisible(true);
                }
                break;
            }
            case 7:
            {
                GameName->setString(pButton->getTitleText());
                for (int i = 0; i<GNS_PlayerMgr::getInstance()->getGameList()->size(); i++) {
					auto st1 = SystemMgr::getInstance()->utf8ToGb(pButton->getTitleText().c_str());
					auto st2 = GNS_PlayerMgr::getInstance()->getGameList()->at(i).szGameName;
                    if (st1 == st2) {
                        GNS_PlayerMgr::getInstance()->getGameProReveRes()->clear();
                        if (DataListView != nullptr)
                            DataListView->removeAllItems();
                        CMD_CS_GetManToManGameRevenue  pGetGameReven;
                        pGetGameReven._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                        pGetGameReven._uplineCode = CurID;
                        pGetGameReven._gameId = GNS_PlayerMgr::getInstance()->getGameId();
                        pGetGameReven._type = 2;
						char TimeDate[11] = { 0 };
						sprintf(TimeDate, "%d-%02d-%02d", Year, Mon, Day);
						std::string TimeDateTmp = SystemMgr::getInstance()->utf8ToGb(TimeDate);
						memcpy(pGetGameReven._selDate, TimeDateTmp.c_str(), 11);
                        GNS_PlayerMgr::getInstance()->setGameId(pGetGameReven._gameId);
                        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMAN_GAME_REVENUE, (char*)&pGetGameReven, sizeof(pGetGameReven));
                        GameType->setVisible(false);
                    }
                }
                break;
            }
            default:
                break;
        }
    }
}
void GNS_DailyDate::AcceptSeverDate()
{
    DataListView->removeAllItems();
    int index =(int)GNS_PlayerMgr::getInstance()->getGameProReveRes()->size();
    int x =0;
    Size bgSize;
    if (index > 0)
    {
        GNS_PlayerMgr::getInstance()->transferEachTime(Year,Mon, Day, GNS_PlayerMgr::getInstance()->getGameProReveRes()->at(0)._selDate);
        TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,Day));
    }
    for (auto it:(*GNS_PlayerMgr::getInstance()->getGameProReveRes())) {
        auto Itembg = Button::create("line between1.png");
        Itembg->setTag(100+x);
        Itembg->addTouchEventListener(CC_CALLBACK_2(GNS_DailyDate::ListViewBtn, this));
        DataListView->addChild(Itembg);
        bgSize = Itembg->getContentSize();
        
        auto Name = Text::create(StringUtils::format("%d",it._lowlineCode), GameFontName, 24);
        Name->setPosition(Vec2(bgSize.width*0.14, bgSize.height*0.34));
        Name->setColor(Color3B::BLACK);
        Itembg->addChild(Name);
        
        auto lineRevenueRatio = Text::create(StringUtils::format("%lld",it._lowlineRevenueRatio), GameFontName, 24);
        lineRevenueRatio->setPosition(Vec2(bgSize.width*0.45, bgSize.height*0.34));
        lineRevenueRatio->setColor(Color3B::BLACK);
        Itembg->addChild(lineRevenueRatio);
        
        auto agentRevenueRatio = Text::create(StringUtils::format("%lld",it._agentRevenueRatio), GameFontName, 24);
        agentRevenueRatio->setPosition(Vec2(bgSize.width*0.8, bgSize.height*0.34));
        agentRevenueRatio->setColor(Color3B::BLACK);
        Itembg->addChild(agentRevenueRatio);
        x++;
    }
    if (bgSize.width!=0||bgSize.height!=0) {
        DataListView->setInnerContainerSize(Size(bgSize.width, (bgSize.height+10)*index));
    }
}
void GNS_DailyDate::AccepPlayerDate() 
{
    if (DataListView != nullptr)
        DataListView->removeAllItems();
    
	pDailyPlayDate->removeAllChildren();
	pDailyPlayDate->setVisible(true);
	pDailyPlayDate->SetNodeName("PvpPlayerDate.csb");
    
	if (!GNS_PlayerMgr::getInstance()->getGamePlayReveRes()->empty())
		CurID = GNS_PlayerMgr::getInstance()->getGamePlayReveRes()->front()._uplineCode;
	pDailyPlayDate->SetProName(CurID);
    
    if (pGNS_DailyDate != nullptr)
        pGNS_DailyDate->setVisible(false);
}
void GNS_DailyDate::TimeButtonCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
        if (type==ui::Widget::TouchEventType::ENDED) {
            auto pButton = (Button*)pSender;
            std::string BtnText = pButton->getTitleText();
            Mon = pCalendar->Mon;
            Year = pCalendar->year;
            Day = atoi(BtnText.c_str());
            TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,atoi(BtnText.c_str())));
            pCalendar->Show(false);
        
            if (DataListView != nullptr)
                DataListView->removeAllItems();
            GNS_PlayerMgr::getInstance()->getGameProReveRes()->clear();
            CMD_CS_GetManToManGameRevenue  pGetGameReven;
            pGetGameReven._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
            pGetGameReven._uplineCode=CurID;
            pGetGameReven._gameId=GNS_PlayerMgr::getInstance()->getGameId();
            pGetGameReven._type=2;
			char TimeDate[11] = { 0 };
			sprintf(TimeDate, "%d-%02d-%02d", Year, Mon, Day);
            std::string TimeDateTmp = SystemMgr::getInstance()->utf8ToGb(TimeDate);
            memcpy(pGetGameReven._selDate, TimeDateTmp.c_str(), 11);
			GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMAN_GAME_REVENUE, (char*)&pGetGameReven, sizeof(pGetGameReven));
        }
}

void GNS_DailyDate::SelectedItemEvents(cocos2d::Ref *pSender, ui::ListView::EventType type)
{
    if (type==ui::ListView::EventType::ON_SELECTED_ITEM_START) {
        auto pListView = (ListView*)pSender;
        int Index = (int)pListView->getCurSelectedIndex();
        int x =0;
        for (auto it:(*GNS_PlayerMgr::getInstance()->getGameProReveRes())) {
            if (Index==x) {
                GNS_PlayerMgr::getInstance()->getGamePlayReveRes()->clear();
                CMD_CS_GetManToManGameRevenue  pGetGameReven;
                pGetGameReven._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                pGetGameReven._uplineCode=it._lowlineCode;
                pGetGameReven._gameId = GNS_PlayerMgr::getInstance()->getGameId();
                pGetGameReven._type=3;
				char TimeDate[11] = { 0 };
				sprintf(TimeDate, "%d-%02d-%02d", Year, Mon, Day);
				std::string TimeDateTmp = SystemMgr::getInstance()->utf8ToGb(TimeDate);
                memcpy(pGetGameReven._selDate, TimeDateTmp.c_str(), 11);
				GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMAN_GAME_REVENUE, (char*)&pGetGameReven, sizeof(pGetGameReven));
                PlayerName=it._lowlineCode;
            }
            x++;
        }
    }
}

void GNS_DailyDate::SetProName(std::string Name)
{
    if (pProName != nullptr)
    {
		std::string strAgentCode =CCGnsOften::getUtf8_DirectAssignmentInProgram("代理商账号:");
		strAgentCode.append(SystemMgr::getInstance()->GbToUTF8(Name.c_str()));
		pProName->setString(strAgentCode);
        CurID=atoi(Name.c_str());
    }
}

void GNS_DailyDate::ClearProName()
{ 
	if (pProName != nullptr) 
	{ 
		pProName->setString(""); 
	} 
}

void GNS_DailyDate::ListViewBtn(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        int x =0;
        for (auto it:(*GNS_PlayerMgr::getInstance()->getGameProReveRes())) {
            if (BtnTag-100==x) {
                GNS_PlayerMgr::getInstance()->getGamePlayReveRes()->clear();
                if (DataListView != nullptr)
                    DataListView->removeAllItems();
                CMD_CS_GetManToManGameRevenue  pGetGameReven;
                pGetGameReven._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                pGetGameReven._uplineCode=it._lowlineCode;
                pGetGameReven._gameId=it._gameId;
                pGetGameReven._type=3;
				char TimeDate[11] = { 0 };
				sprintf(TimeDate, "%d-%02d-%02d", Year, Mon, Day);
				std::string TimeDateTmp = SystemMgr::getInstance()->utf8ToGb(TimeDate);
				memcpy(pGetGameReven._selDate, TimeDateTmp.c_str(), 11);
                GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMAN_GAME_REVENUE, (char*)&pGetGameReven, sizeof(pGetGameReven));
                PlayerName=it._lowlineCode;
				ClearProName();
            }
            x++;
        }
    }
}

void GNS_DailyDate::onExit()
{
    Layer::onExit();
}